package top.infopub.job.fia.service.impl;


import com.alibaba.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.job.bpa.service.BizcostApplyService;
import top.infopub.job.cap.domain.PayfeeApplyDomain;
import top.infopub.job.cap.domain.RpiApplyDomain;
import top.infopub.job.cap.service.PayfeeApplyService;
import top.infopub.job.cap.service.RpiApplyService;
import top.infopub.job.fia.dao.PayBillDao;
import top.infopub.job.fia.domain.TaxApplyDomain;
import top.infopub.job.fia.service.PayBillDetailService;
import top.infopub.job.fia.service.PayBillService;
import top.infopub.job.fia.service.TaxApplyService;
import top.infopub.job.mkt.service.BidBondApplyService;
import top.infopub.job.om.domain.PaymentApplyDomain;
import top.infopub.job.om.service.PaymentApplyService;
import top.infopub.model.bpa.BizcostApply;
import top.infopub.model.cap.PayfeeApplyDetail;
import top.infopub.model.cap.RpiApplyDetail;
import top.infopub.model.fia.PayBill;
import top.infopub.model.fia.PayBillDetail;
import top.infopub.model.fia.TaxApplyDetail;
import top.infopub.model.mkt.BidBondApply;
import top.infopub.model.om.PaymentApplyDetail;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class PayBillServiceImpl extends GenericServiceImpl<PayBill, String> implements PayBillService {

    @Autowired
    private PayBillDao payBillDao;

    @Autowired
    private PayBillDetailService payBillDetailService;

    @Autowired
    private BidBondApplyService bidBondApplyService;

    @Autowired
    private PayfeeApplyService payfeeApplyService;

    @Autowired
    private RpiApplyService rpiApplyService;

    @Autowired
    private BizcostApplyService bizcostApplyService;

    @Autowired
    private TaxApplyService taxApplyService;

    @Autowired
    private PaymentApplyService paymentApplyService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Override
    public GenericDao<PayBill, String> getDao() {
        return payBillDao;
    }

    @Override
    public BigDecimal selectProQaOfCashOut(Map<String, Object> where) {
        return payBillDao.selectProQaOfCashOut(where);
    }

    @Override
    public BigDecimal selectProSingleOfCashOut(Map<String, Object> where) {
        BigDecimal rs = payBillDao.selectProSingleOfCashOut(where);
        if (rs == null) {
            return BigDecimal.ZERO;
        }
        return rs;
    }

    @Override
    public BigDecimal selectAreaQaOfCashOut(Map<String, Object> where) {
        return payBillDao.selectAreaQaOfCashOut(where);
    }

    @Override
    public BigDecimal selectAllProQaOfCashOut(Map<String, Object> where) {
        return payBillDao.selectAllProQaOfCashOut(where);
    }

    @Override
    public BigDecimal selectProQaOfInvest(Map<String, Object> where) {
        return payBillDao.selectProQaOfInvest(where);
    }

    @Override
    public BigDecimal selectProSingleOfInvest(Map<String, Object> where) {
        BigDecimal rs = payBillDao.selectProSingleOfInvest(where);
        if (rs == null) {
            return BigDecimal.ZERO;
        }
        return rs;
    }

    @Override
    public BigDecimal selectAreaQaOfInvest(Map<String, Object> where) {
        return payBillDao.selectAreaQaOfInvest(where);
    }

    @Override
    public BigDecimal selectAllProQaOfInvest(Map<String, Object> where) {
        return payBillDao.selectAllProQaOfInvest(where);
    }

    @Override
    public BigDecimal selectProQaOfTaxFee(Map<String, Object> where) {
        return payBillDao.selectProQaOfTaxFee(where);
    }

    @Override
    public BigDecimal selectProQaOfFinSpread(Map<String, Object> where) {
        return payBillDao.selectProQaOfFinSpread(where);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payBillJob() {
        // 1.市场投标保证金
        bidBondApply();
        // 2.融资付款申请
        payfeeApply();
        // 3.融资还本付息
        rpiApply();
        // 4.商务支付申请
        bizcostApply();
        // 5.财务税费申请
        taxApply();
        // 6.运营付款申请
        paymentApply();
    }

    private void bidBondApply() {
        List<BidBondApply> list = bidBondApplyService.selectNeedToPayBill();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (BidBondApply bidBondApply : list) {
            PayBill model = new PayBill();
            String id = IdFactory.getUUIDUpper();
            model.setId(id);
            model.setCodes(codeGeneratorConfigService.generatorCode("fia_pay_bill"));
            model.setProCode(bidBondApply.getProCode());
//            model.setProName(bidBondApply.getProName());
//            model.setFromType("1");
//            model.setFromTableName("MKT_BID_BOND_APPLY");
//            model.setFromSguid(bidBondApply.getId());
            model.setWillPayAmount(bidBondApply.getPayTotal());//应付总额（元）
//            model.setState(0);//待登记
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            payBillDao.insertById(model);

            PayBillDetail payBillDetail = new PayBillDetail();
            payBillDetail.setPayBillId(id);
            payBillDetail.setAccountName(bidBondApply.getAccountName());
            payBillDetail.setApplyAmount(bidBondApply.getPayTotal());//申请付款金额
            payBillDetail.setAddBy("system");
            payBillDetail.setAddTime(new Date());
            payBillDetailService.insert(payBillDetail);
        }
    }

    private void payfeeApply() {
        List<PayfeeApplyDomain> list = payfeeApplyService.selectNeedToPayBill();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (PayfeeApplyDomain payfeeApplyDomain : list) {
            PayBill model = new PayBill();
            String id = IdFactory.getUUIDUpper();
            model.setId(id);
            model.setCodes(codeGeneratorConfigService.generatorCode("fia_pay_bill"));
            model.setProCode(payfeeApplyDomain.getProCode());
//            model.setProName(payfeeApplyDomain.getProName());
//            model.setFromType("2");
//            model.setFromTableName("CAP_PAYFEE_APPLY");
//            model.setFromSguid(payfeeApplyDomain.getId());
            model.setWillPayAmount(payfeeApplyDomain.getPayTotal());//应付总额（元）
//            model.setHrOrgBillId(payfeeApplyDomain.getHrOrgBillId());//付款公司id
//            model.setState(0);//待登记
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            payBillDao.insertById(model);

            if (CollectionUtils.isNotEmpty(payfeeApplyDomain.getPayfeeApplyDetails())) {
                List<PayBillDetail> payBillDetails = new ArrayList<PayBillDetail>();
                for (PayfeeApplyDetail payfeeApplyDetail : payfeeApplyDomain.getPayfeeApplyDetails()) {
                    PayBillDetail payBillDetail = new PayBillDetail();
                    payBillDetail.setPayBillId(id);
                    payBillDetail.setId(IdFactory.getUUIDUpper());
                    payBillDetail.setAccountName(payfeeApplyDetail.getAccountName());
                    payBillDetail.setApplyAmount(payfeeApplyDetail.getPaySingle());//申请付款金额
                    payBillDetail.setAddBy("system");
                    payBillDetail.setAddTime(new Date());
                    payBillDetails.add(payBillDetail);
                }
                payBillDetailService.insertByBatch(payBillDetails);
            }
        }
    }

    private void rpiApply() {
        List<RpiApplyDomain> list = rpiApplyService.selectNeedToPayBill();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (RpiApplyDomain rpiApplyDomain : list) {
            PayBill model = new PayBill();
            String id = IdFactory.getUUIDUpper();
            model.setId(id);
            model.setCodes(codeGeneratorConfigService.generatorCode("fia_pay_bill"));
            model.setProCode(rpiApplyDomain.getProCode());
//            model.setProName(rpiApplyDomain.getProName());
//            model.setFromType("3");
//            model.setFromTableName("CAP_RPI_APPLY");
//            model.setFromSguid(rpiApplyDomain.getId());
            model.setWillPayAmount(rpiApplyDomain.getPayTotal());//应付总额（元）
//            model.setHrOrgBillId(rpiApplyDomain.getHrOrgBillId());//付款公司id
//            model.setState(0);//待登记
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            payBillDao.insertById(model);

            if (CollectionUtils.isNotEmpty(rpiApplyDomain.getRpiApplyDetails())) {
                List<PayBillDetail> payBillDetails = new ArrayList<PayBillDetail>();
                for (RpiApplyDetail detail : rpiApplyDomain.getRpiApplyDetails()) {
                    PayBillDetail payBillDetail = new PayBillDetail();
                    payBillDetail.setPayBillId(id);
                    payBillDetail.setId(IdFactory.getUUIDUpper());
                    payBillDetail.setAccountName(detail.getAccountName());
                    payBillDetail.setRepayType(detail.getPayType());
                    payBillDetail.setApplyAmount(detail.getPaySingle());//申请付款金额
                    payBillDetail.setAddBy("system");
                    payBillDetail.setAddTime(new Date());
                    payBillDetails.add(payBillDetail);
                }
                payBillDetailService.insertByBatch(payBillDetails);
            }
        }
    }

    private void bizcostApply() {
        List<BizcostApply> list = bizcostApplyService.selectNeedToPayBill();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (BizcostApply apply : list) {
            PayBill model = new PayBill();
            String id = IdFactory.getUUIDUpper();
            model.setId(id);
            model.setCodes(codeGeneratorConfigService.generatorCode("fia_pay_bill"));
            model.setProCode(apply.getProCode());
//            model.setProName(apply.getProName());
//            model.setFromType("4");
//            model.setFromTableName("BPA_BIZCOST_APPLY");
//            model.setFromSguid(apply.getId());
            model.setWillPayAmount(apply.getPayTotal());//应付总额（元）
//            model.setState(0);//待登记
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            payBillDao.insertById(model);

            PayBillDetail payBillDetail = new PayBillDetail();
            payBillDetail.setPayBillId(id);
            payBillDetail.setAccountName(apply.getCoopName());
            payBillDetail.setApplyAmount(apply.getPayTotal());//申请付款金额
            payBillDetail.setAddBy("system");
            payBillDetail.setAddTime(new Date());
            payBillDetailService.insert(payBillDetail);
        }
    }

    private void taxApply() {
        List<TaxApplyDomain> list = taxApplyService.selectNeedToPayBill();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (TaxApplyDomain apply : list) {
            PayBill model = new PayBill();
            String id = IdFactory.getUUIDUpper();
            model.setId(id);
            model.setCodes(codeGeneratorConfigService.generatorCode("fia_pay_bill"));
            model.setProCode(apply.getProCode());
//            model.setProName(apply.getProName());
//            model.setFromType("5");
//            model.setFromTableName("FIA_TAX_APPLY");
//            model.setFromSguid(apply.getId());
            model.setWillPayAmount(apply.getApplySumAmount());//应付总额（元）
//            model.setHrOrgBillId(apply.getHrOrgBillId());//付款公司id
//            model.setState(0);//待登记
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            payBillDao.insertById(model);

            if (CollectionUtils.isNotEmpty(apply.getTaxApplyDetails())) {
                List<PayBillDetail> payBillDetails = new ArrayList<PayBillDetail>();
                for (TaxApplyDetail detail : apply.getTaxApplyDetails()) {
                    PayBillDetail payBillDetail = new PayBillDetail();
                    payBillDetail.setPayBillId(id);
                    payBillDetail.setId(IdFactory.getUUIDUpper());
                    payBillDetail.setAccountName("无");
                    payBillDetail.setTaxType(detail.getTaxType());
                    payBillDetail.setApplyAmount(detail.getTaxAmount());//申请付款金额
                    payBillDetail.setAddBy("system");
                    payBillDetail.setAddTime(new Date());
                    payBillDetails.add(payBillDetail);
                }
                payBillDetailService.insertByBatch(payBillDetails);
            }
        }
    }

    private void paymentApply() {
        List<PaymentApplyDomain> list = paymentApplyService.selectNeedToPayBill();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (PaymentApplyDomain apply : list) {
            PayBill model = new PayBill();
            String id = IdFactory.getUUIDUpper();
            model.setId(id);
            model.setCodes(codeGeneratorConfigService.generatorCode("fia_pay_bill"));
            model.setProCode(apply.getProCode());
//            model.setProName(apply.getProName());
//            model.setHrOrgBillId(apply.getHrOrgBillId());
//            model.setFromType("6");
//            model.setFromTableName("OM_PAYMENT_APPLY");
//            model.setFromSguid(apply.getId());
            model.setWillPayAmount(apply.getPayTotal());//应付总额（元）
//            model.setState(0);//待登记
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            payBillDao.insertById(model);

            if (CollectionUtils.isNotEmpty(apply.getPaymentApplyDetails())) {
                List<PayBillDetail> payBillDetails = new ArrayList<PayBillDetail>();
                for (PaymentApplyDetail detail : apply.getPaymentApplyDetails()) {
                    PayBillDetail payBillDetail = new PayBillDetail();
                    payBillDetail.setPayBillId(id);
                    payBillDetail.setId(IdFactory.getUUIDUpper());
                    payBillDetail.setAccountName(detail.getAccountName());
                    payBillDetail.setApplyAmount(detail.getPaySingle());//申请付款金额
                    payBillDetail.setAddBy("system");
                    payBillDetail.setAddTime(new Date());
                    payBillDetails.add(payBillDetail);
                }
                payBillDetailService.insertByBatch(payBillDetails);
            }
        }
    }
}